import {Component, OnInit} from '@angular/core';
import {MagicScienceInnovateService} from '../magic-science-innovate/magic-science-innovate.service';
import {RegionPolicyCondition} from '../content/model/region-policy-condition';
import {NewPolicy} from '../content/model/new-policy';
import {ActivatedRoute, Params} from '@angular/router';
import {NewPolicyService} from '../content/new-policy/new-policy.service';
import * as echarts from 'echarts';
import 'echarts-wordcloud';
import {AreaAnalysisCondition} from '../content/model/area-analysis-condition';

@Component({
  selector: 'app-new-policy-list',
  templateUrl: './new-policy-list.component.html',
  styleUrls: ['./new-policy-list.component.less']
})
export class NewPolicyListComponent implements OnInit {
  getListCondition : RegionPolicyCondition = new RegionPolicyCondition();
  //getListCondition2 : { pageNum: number, pageSize:number, total:number, region:string };
  getListCondition2 = { pageNum: 1, pageSize:10, total:1, region:'' };
  policyList: NewPolicy[] = [];
  week: { name: string, value: number }[] = [];
  isSpinning;
  wordCloudList = [];
  areaAnalysis = new AreaAnalysisCondition();

  selectedWeek = 0;

  constructor(private magicSService: MagicScienceInnovateService,
              private route: ActivatedRoute,
              private newService: NewPolicyService) {
  }

  ngOnInit(): void {
    this.route.queryParams.pipe().subscribe((params: Params) => {
      if (params.opt) {
            this.getListCondition2 = JSON.parse(params.opt);
            this.getListCondition2['pageSize'] = 10;
            this.getListCondition2['notCount'] = true;      // 这样就不返回右侧的关键词图标数据, 性能高。
            
      }
      if (params.region) {
            this.getListCondition2['region'] = params.region;
      }
      console.log('[ngOnInit]', this.getListCondition2);
    });

    this.getRegionPolicyList();
    // this.getWordCloud();
  }

  getRegionPolicyList() {
    this.isSpinning = true;
    this.magicSService.getPolicySortRegion(this.getListCondition2).subscribe(res => {
      if( res.result[ this.getListCondition2.region ] ){
          let res2 = res.result[ this.getListCondition2.region ];
          this.policyList = res2.policys.list;
          this.getListCondition2.total = res2.policys.total;
          this.getListCondition2.pageNum = res2.policys.pageNum;
          this.getListCondition2.pageSize = res2.policys.pageSize;
      }
      this.isSpinning = false;
    });

  }

// 根据select子组件传来的condition查询政策表格数据
  queryTable(condition: AreaAnalysisCondition) {
    this.areaAnalysis = condition;
  }

  getWordCloud() {
    this.isSpinning = true;
    this.newService.getWordCloud(this.getListCondition).subscribe(res => {
      // this.wordCloudList = res.result;
      //res.result = res.result.reverse();    // 坑: 后台是从多到少返回的, 但图表显示时却反着。这里数据给翻转下。
      const nameList = [];
      const valueList = [];
      for (const resultItem of res.result) {
        nameList.push(resultItem.name);
      }
      // this.setWordCloud(nameList, res.result);
      this.isSpinning = false;
    });
  }

  // 页码改变时回调
  pageChange(event: number) {
    this.getListCondition2.pageNum = event;
    this.getRegionPolicyList();
    //this.getWordCloud();
  }

}
